home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / pbmplus / pbm / pbmtomacp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  6.2 KB  |  298 lines

  1. /* pbmtomacp.c - read a portable bitmap and produce a MacPaint bitmap file
  2. **
  3. ** Copyright (C) 1988 by Douwe vand der Schaaf.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "pbm.h"
  14. #include "macp.h"
  15.  
  16. #define TRUE        1
  17. #define FALSE        0
  18. #define EQUAL        1
  19. #define UNEQUAL        0
  20.  
  21. static void fillbits ARGS(( bit **bits, bit **bitsr, int top, int left, int bottom, int right ));
  22. static void writemacp ARGS(( bit **bits ));
  23. static int packit ARGS(( bit *pb, bit *bits ));
  24. static void filltemp ARGS(( bit *dest, bit *src ));
  25. static void sendbytes ARGS(( bit *pb, register int npb ));
  26. static void header ARGS(( void ));
  27.  
  28. static FILE *fdout;
  29.  
  30. void main(argc, argv)
  31. int argc;
  32. char *argv[];
  33. { FILE *ifp;
  34.   register bit **bits, **bitsr;
  35.   int argn, rows, cols;
  36.   int left,bottom,right,top;
  37.   int lflg, rflg, tflg, bflg;
  38.   char name[100];
  39.   char *usage = "[-l left] [-r right] [-b bottom] [-t top] [pbmfile]";
  40.  
  41.   pbm_init( &argc, argv );
  42.  
  43.   argn = 1;
  44.   fdout = stdout;
  45.   lflg = rflg = tflg = bflg = 0;
  46.  
  47.   while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
  48.   { switch ( argv[argn][1] )
  49.     { case 'l':
  50.       lflg++;
  51.       argn++;
  52.       left = atoi( argv[argn] );
  53.       break;
  54.  
  55.       case 'r':
  56.       rflg++;
  57.       argn++;
  58.       right = atoi( argv[argn] );
  59.       break;
  60.  
  61.       case 't':
  62.       tflg++;
  63.       argn++;
  64.       top = atoi( argv[argn] );
  65.       break;
  66.  
  67.       case 'b':
  68.       bflg++;
  69.       argn++;
  70.       bottom = atoi( argv[argn] );
  71.       break;
  72.  
  73.       case '?':
  74.       default:
  75.       pm_usage( usage );
  76.     }
  77.     ++argn;
  78.   }
  79.  
  80.   if ( argn == argc )
  81.   { ifp = stdin;
  82.     strcpy( name, "noname" );
  83.   }
  84.   else
  85.   { ifp = pm_openr( argv[argn] );
  86.     strcpy( name, argv[argn] );
  87.     ++argn;
  88.   }
  89.  
  90.   if ( argn != argc )
  91.     pm_usage( usage );
  92.  
  93.   bitsr = pbm_readpbm( ifp, &cols, &rows );
  94.  
  95.   pm_close( ifp );
  96.  
  97.   bits = pbm_allocarray( MAX_COLS, MAX_LINES );
  98.  
  99.   if( !lflg )
  100.     left = 0;
  101.  
  102.   if( rflg )
  103.   { if( right - left >= MAX_COLS )
  104.       right = left + MAX_COLS - 1;
  105.   }
  106.   else
  107.     right = ( left + MAX_COLS > cols ) ? ( cols - 1 ) : ( left + MAX_COLS - 1 );
  108.  
  109.   if( !tflg )
  110.     top = 0;
  111.  
  112.   if( bflg )
  113.   { if( bottom - top >= MAX_LINES )
  114.       bottom = top + MAX_LINES - 1;
  115.   }
  116.   else
  117.     bottom = ( top + MAX_LINES > rows ) ?
  118.            ( rows - 1 ) : ( top + MAX_LINES - 1 );
  119.   
  120.     if( right <= left || left < 0 || right - left + 1 > MAX_COLS )
  121.       pm_error("error in right (= %d) and/or left (=%d)",right,left );
  122.     if( bottom <= top || top < 0 || bottom - top + 1 > MAX_LINES )
  123.       pm_error("error in bottom (= %d) and/or top (=%d)",bottom,top );
  124.  
  125.   fillbits( bits, bitsr, top, left, bottom, right );
  126.  
  127.   writemacp( bits );
  128.  
  129.   exit( 0 );
  130.  
  131. }
  132.  
  133. /* - - - - - - - - - - - - - - - - - - - - - - - - - - */
  134.  
  135. /* centreer het over te zenden plaatje in het MacPaint document
  136.  *
  137.  * Het plaatje wordt vanaf al of niet opgegeven (left, bottom)
  138.  * in een pbm bitmap van de juist macpaint afmetingen gezet,
  139.  * en eventueel afgekapt.
  140.  */
  141. static void
  142. fillbits( bits, bitsr, top, left, bottom, right )
  143. bit **bits, **bitsr;
  144. int top, left, bottom, right;
  145. { register bit *bi, *bir;
  146.   register int i, j;
  147.   register int bottomr, leftr, topr, rightr;
  148.   int width, height;
  149.  
  150.   width = right - left + 1;
  151.   leftr = (MAX_COLS - width) / 2;
  152.   rightr = leftr + width - 1;
  153.  
  154.   height = bottom - top + 1;
  155.   topr = ( MAX_LINES - height ) / 2;
  156.   bottomr = topr + height - 1;
  157.  
  158.   for( i = 0; i < topr; i++ )
  159.   { bi = bits[i];
  160.     for( j = 0; j < MAX_COLS; j++ )
  161.       *bi++ = 0;
  162.   }
  163.  
  164.   for( i = topr; i <= bottomr; i++ )
  165.   { bi = bits[i];
  166.     { for( j = 0; j < leftr; j++ )
  167.     *bi++ = 0;
  168.       bir = bitsr[ i - topr + top ];
  169.       for( j = leftr; j <= rightr; j++ )
  170.     *bi++ = bir[j - leftr + left];
  171.       for( j = rightr + 1; j < MAX_COLS; j++ )
  172.     *bi++ = 0;
  173.   } }
  174.  
  175.   for( i = bottomr + 1; i < MAX_LINES; i++ )
  176.   { bi = bits[i];
  177.     for( j = 0; j < MAX_COLS; j++ )
  178.       *bi++ = 0;
  179.   }
  180. } /* fillbits */
  181.       
  182. /* - - - - - - - - - - - - - - - - - - - - - - - - - - */
  183.  
  184. static void
  185. writemacp( bits )
  186. bit **bits;
  187. { register int i;
  188.   bit pb[MAX_COLS * 2];
  189.   int npb;
  190.  
  191.   header();
  192.   for( i=0; i < MAX_LINES; i++ )
  193.   { npb = packit( pb, bits[i] );
  194.     sendbytes( pb, npb );
  195.   }
  196. } /* writemacp */
  197.  
  198. /* - - - - - - - - - - - - - - - - - - - - - - - - - - */
  199.  
  200. /* pack regel van MacPaint doc in Apple's format
  201.  * return value = # of bytes in pb 
  202.  */
  203. static int
  204. packit( pb, bits )
  205. bit *pb, *bits;
  206. { register int charcount, npb, newcount, flg;
  207.   bit temp[72];
  208.   bit *count, *srcb, *destb, save;
  209.  
  210.   srcb = bits; destb = temp;
  211.   filltemp( destb, srcb );
  212.   srcb = temp;
  213.   destb = pb;
  214.   npb = 0;
  215.   charcount = BYTES_WIDE;
  216.   flg = EQUAL;
  217.   while( charcount )
  218.   { save = *srcb++;
  219.     charcount--;
  220.     newcount = 1;
  221.     while( (*srcb == save) && charcount )
  222.     { srcb++;
  223.       newcount++;
  224.       charcount--;
  225.     }
  226.     if( newcount > 2 )
  227.     { count = destb++;
  228.       *count = 257 - newcount;
  229.       *destb++ = save;
  230.       npb += 2;
  231.       flg = EQUAL;
  232.     }
  233.     else
  234.     { if( flg == EQUAL )
  235.       { count = destb++;
  236.     *count = newcount - 1;
  237.     npb++;
  238.       }
  239.       else
  240.     *count += newcount;
  241.       while( newcount-- )
  242.       { *destb++ = save;
  243.         npb++;
  244.       }
  245.       flg = UNEQUAL;
  246.   } }
  247.   return npb;
  248. } /* packit */
  249.  
  250. /* - - - - - - - - - - - - - - - - - - - - - - - - - - */
  251.  
  252. static void
  253. filltemp( dest, src )
  254. bit *dest, *src;
  255. { register unsigned char ch, zero, acht;
  256.   register int i, j;
  257.  
  258.   zero = '\0';
  259.   acht = 8;
  260.   i = BYTES_WIDE;
  261.   while( i-- )
  262.   { ch = zero; 
  263.     j = acht;
  264.     while( j-- )
  265.     { ch <<= 1;
  266.       if( *src++ )
  267.     ch++;
  268.     }
  269.     *dest++ = ch;
  270.   }
  271. } /* filltemp */
  272.  
  273. /* - - - - - - - - - - - - - - - - - - - - - - - - - - */
  274.  
  275. static void
  276. sendbytes( pb, npb )
  277. bit *pb;
  278. register int npb;
  279. { register bit *b;
  280.  
  281.   b = pb;
  282.   while( npb-- )
  283.     (void) putc( *b++, fdout );
  284. } /* sendbytes */
  285.  
  286. /* - - - - - - - - - - - - - - - - - - - - - - - - - - */
  287.  
  288. static void
  289. header()
  290. { register int i;
  291.   register char ch;
  292.  
  293.   /* header contains nothing ... */
  294.   ch = '\0';
  295.   for(i = 0; i < HEADER_LENGTH; i++ )
  296.     (void) putc( ch, fdout );
  297. } /* header */
  298.